import { prisma } from './prisma'

export async function getChildBalance(childId: number): Promise<number> {
  const ledger = await prisma.pointsLedger.findMany({ where: { childId } })
  let earn = 0
  let spend = 0
  let adjust = 0
  for (const l of ledger) {
    if (l.kind === 'earn') earn += l.amount
    else if (l.kind === 'spend') spend += l.amount
    else adjust += l.amount
  }
  return earn - spend + adjust
}


